package com.zw.admin.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.zw.admin.server.dao.GuardDao;
import com.zw.admin.server.dto.ChartDto;
import com.zw.admin.server.dto.GuardSearchDto;
import com.zw.admin.server.dto.chart.GuardChart;
import com.zw.admin.server.model.Guard;
import com.zw.admin.server.service.GuardService;
import com.zw.admin.server.utils.HttpPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/guard")
public class GuardController {

    @Autowired
    private GuardService guardService;
    @Autowired
    private GuardDao guardDao;

    @GetMapping("/getAllOrgName")
    public List<String> getAllOrgName() throws Exception {
        return guardService.getAllOrgName();
    }

    @GetMapping("/getGuardInfoByOrg")
    public List<Guard> getGuardInfoByOrg(String orgName) {
        return guardService.getGuardInfoByOrg(orgName);
    }

    @GetMapping("/updateDelayTiem")
    public String updateDelayTiem(String delayTime) {
        return guardService.updateDelayTiem(delayTime);
    }

    @GetMapping("/updateEarlyExitTime")
    public String updateEarlyExitTime(String earlyExitTime) {
        return guardService.updateEarlyExitTime(earlyExitTime);
    }

    @GetMapping("/getDelayTime")
    public String getDelayTime() {
        return guardService.getDelayTime();
    }

    @GetMapping("/getEarlyExitTime")
    public String getEarlyExitTime() {
        return guardService.getEarlyExitTime();
    }

    @GetMapping("/getAllGuardInfo")
    public List<Guard> getAllGuardInfo() {
        return guardService.getAllGuardInfo();
    }

    @PostMapping("/getGuardInfoByTime")
    public String getGuardInfoByTime(@RequestBody GuardSearchDto searchDto) {
        System.out.println(searchDto);
        JSONObject res = new JSONObject();

        if (searchDto.getOrgNames().isEmpty()){
            res.put("list", guardService.getGuardInfoByTime(searchDto.getStartTime(), searchDto.getEndTime(), searchDto.getLimit(), searchDto.getPageNum()));
            res.put("total", guardDao.getGuardInfoCount(searchDto.getStartTime(), searchDto.getEndTime()));
        }else{
            res.put("list", guardService.getGuardByTimeAndOrg(searchDto.getStartTime(), searchDto.getEndTime(), searchDto.getLimit(), searchDto.getPageNum(),searchDto.getOrgNames()));
            res.put("total", guardDao.getGuardCountByTimeAndOrg(searchDto.getStartTime(), searchDto.getEndTime(),searchDto.getOrgNames()));
        }

        return res.toJSONString();
    }

    @GetMapping("/getCharInfo")
    public String getCharInfo(String startTime,String endTime) {
        List<GuardChart> guardChartList = guardDao.getChartData(startTime,endTime);
        List<String> orgNameList = guardDao.getAllOrgName();
        ArrayList<ChartDto> result = new ArrayList<>();

        for (String orgName : orgNameList) {
            ChartDto chartDto = new ChartDto();
            chartDto.setName(orgName);
            chartDto.setType("line");
            chartDto.setStack("打卡人数");
            chartDto.setData(new ArrayList<ArrayList<String>>());
            result.add(chartDto);
        }

        for (GuardChart gc : guardChartList) {
            for (ChartDto chartDto : result) {
                if ((!(gc.getOrgName()==null) && !(chartDto.getName()==null)) && gc.getOrgName().equals(chartDto.getName())) {
                    ArrayList<String> data = new ArrayList<>();
                    data.add(gc.getDate());
                    data.add(gc.getCheckNum());

                    chartDto.getData().add(data);
                }
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orgList", guardDao.getAllOrgName());
        jsonObject.put("chartData", result);

        return jsonObject.toJSONString();
    }

}
